package mediator
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	import com.greensock.easing.Linear;
	
	import components.core.IComponent;
	import components.core.UIComponent;
	import components.managers.PopUpManager;
	import components.utils.EventListenerUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 *基础
	 * @author zn
	 *
	 */
	public class BaseMediator extends Mediator implements IMediator
	{
		protected var _popUp:Boolean = true;
		
		public var childMedList:Array = [];
		
		public var destoryCallback:Function = null;
		
		public var isCreateCompete:Boolean=false;
		
		public function BaseMediator(name:String, viewComponent:Object = null)
		{
			super(name, viewComponent);
		}
		
		protected function get uiComp():DisplayObject
		{
			return viewComponent as DisplayObject;
		}
		
		/**
		 *显示界面
		 *
		 */
		public function show():void
		{
			if (isShow)
				return;
			
			if (uiComp is UIComponent)
			{
				if ((uiComp as UIComponent).isCreateComplete)
					uiCompCreateComplete(null);
				else
					EventListenerUtil.addEventListener(uiComp, Event.COMPLETE, uiCompCreateComplete);
			}
			else
				addUIComp();
		}
		
		protected function uiCompCreateComplete(event:Event):void
		{
			isCreateCompete=true;
			EventListenerUtil.removeListenerFunc(uiComp, Event.COMPLETE, uiCompCreateComplete);
			addUIComp();
		}
		
		protected function addUIComp():void
		{
			if (_popUp)
			{
				if (uiComp.scaleX == 1)
				{
					uiComp.scaleX = uiComp.scaleY = 0.6;
					uiComp.alpha = 0.5;
				}
				
				PopUpManager.addPopUp(uiComp);
				PopUpManager.centerPopUp(uiComp);
				
				TweenLite.to(uiComp, 0.2, { transformAroundCenter: { scaleX: 1, scaleY: 1, alpha: 1 }, ease: Linear.easeNone });
			}
			else
                MainMediator(getMediator(MainMediator)).addChild(uiComp);
		}
		
		
		public function destroy():void
		{
			if (_popUp)
			{
				TweenLite.to(uiComp, 0.2, { transformAroundCenter: { scaleX: 0.6, scaleY: 0.6, alpha: 0.5 }, ease: Linear.easeNone, onComplete: function():void
				{
					PopUpManager.removePopUp(uiComp);
					
					if (uiComp is IComponent)
						(uiComp as IComponent).destroy();
					viewComponent = null;
					
					callDestoryCallBack();
				}});
			}
			else
			{
				if (uiComp is IComponent)
					(uiComp as IComponent).destroy();
				viewComponent = null;
				
				callDestoryCallBack();
			}
			
			removeCWList();
			
			while (childMedList.length > 0)
				sendNotification(childMedList.pop());
			
			childMedList = null;
			
			facade.removeMediator(getMediatorName());
		}
		
		public function get isShow():Boolean
		{
			return uiComp.stage == null ? false : true;
		}
		
		private function callDestoryCallBack():void
		{
			if (destoryCallback != null)
				destoryCallback();
		}
	}
}
